package com.yztc.xiami_app.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.bean.shopcart.ShopCart;
import com.yztc.xiami_app.constant.OtherConstant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "订单"
 */
public class OrderActivity extends AppCompatActivity {

    @BindView(R.id.activity_order_goods_num)
    TextView goodsNumTv; //商品总数量
    @BindView(R.id.order_goods1_pic_iv)
    ImageView goods1PicIv; //商品图片1，可以为空
    @BindView(R.id.order_goods2_pic_iv)
    ImageView goods2PicIv; //商品图片2，可以为空
    @BindView(R.id.order_goods3_pic_iv)
    ImageView goods3PicIv; //商品图片3，可以为空
    @BindView(R.id.order_goods4_pic_iv)
    ImageView goods4PicIv; //商品图片4，可以为空
    @BindView(R.id.activity_order_totalPrice)
    TextView totalPriceTv; //商品总价
    @BindView(R.id.payable_price)
    TextView payablePriceTv; //应付金额
    @BindView(R.id.discount_price)
    TextView discountPriceTv; //优惠金额
    @BindView(R.id.discount_coupon_tv)
    TextView discountCouponTv; //优惠券，此处默认与优惠金额相同


    private Context mContext;
    private List<ShopCart> shopCartList;  //购物车中选中的商品的集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        ButterKnife.bind(this);

        mContext = this;

        getGoodsPicUrl();
        loadGoodsPic();
        loadGoodsNumAndPrice();
    }

    //加载商品的总价，折扣，以及商品的数量
    private void loadGoodsNumAndPrice() {
        //设置商品的总数量
        goodsNumTv.setText(shopCartList.size() + "件商品");
        //设置商品的总价
        int totalPrice = 0;
        for (ShopCart shopCart : shopCartList) {
            int price = shopCart.getPrice();
            totalPrice += price;
        }
        totalPriceTv.setText("¥" + totalPrice); //设置总价
        //设置优惠金额,默认打9折
        int discount = (int) (totalPrice * 0.1);
        discountPriceTv.setText("优惠金额：¥" + discount);
        //设置应付金额
        int payablePrice = totalPrice - discount;
        payablePriceTv.setText("应付金额：¥" + payablePrice);
        //设置优惠券
        discountCouponTv.setText("-¥"+discount);
    }

    //加载商品的图片，最多4个，多余的则省略
    private void loadGoodsPic() {
        for (int i = 0; i < shopCartList.size(); i++) {
            switch (i) {
                case 0:
                    Glide.with(mContext).load(shopCartList.get(i).getGoodsPic()).into(goods1PicIv);
                    break;
                case 1:
                    Glide.with(mContext).load(shopCartList.get(i).getGoodsPic()).into(goods2PicIv);
                    break;
                case 2:
                    Glide.with(mContext).load(shopCartList.get(i).getGoodsPic()).into(goods3PicIv);
                    break;
                case 3:
                    Glide.with(mContext).load(shopCartList.get(i).getGoodsPic()).into(goods4PicIv);
                    break;
            }

        }

    }

    //获取“购物车”页面传递的商品图片
    private void getGoodsPicUrl() {
        Intent intent = getIntent();
        //商品图片的集合
        shopCartList = (List<ShopCart>) intent.getSerializableExtra(OtherConstant.KEY_GOODS_TO_ORDER);
    }

}
